package com.cn.por.util.pdf;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class PdfParser {

    public static void main(String[] args) {
        String pdfFilePath = "D:\\auto\\temp\\ll.pdf";
        String textFilePath = "D:\\auto\\temp\\ll.txt";

        try {
            // Load PDF document
            PdfReader reader = new PdfReader(pdfFilePath);
            PdfDocument pdfDocument = new PdfDocument(reader);

            // Output text file
            File file = new File(textFilePath);
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));

            // Extract text from each page and write to file
            int numPages = pdfDocument.getNumberOfPages();
            for (int pageNum = 1; pageNum <= numPages; pageNum++) {
                String pageText = PdfTextExtractor.getTextFromPage(pdfDocument.getPage(pageNum));
                writer.write(pageText);
                writer.newLine();
            }

            // Close resources
            writer.close();
            pdfDocument.close();

            System.out.println("Text extracted and saved to " + textFilePath);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
